import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart'; // 新增这行，Locale 类来自该包

class LocalizationService extends Translations {
  Locale get locale => Get.deviceLocale ?? const Locale('en', 'US');

  static final fallbackLocale = const Locale('en', 'US');

  @override
  Map<String, Map<String, String>> get keys => _keys;

  static Map<String, Map<String, String>> _keys = {};

  LocalizationService() {
    // Load JSONs into keys map on construction (synchronous small files)
    _load();
  }

  void _load() {
    // In this skeleton we will load assets synchronously by decoding them once via rootBundle.
    // When bundling, assets are available.
    rootBundle.loadString('assets/i18n/zh_CN.json').then((s) {
      final m = Map<String, dynamic>.from(json.decode(s));
      _keys['zh_CN'] = m.map((k, v) => MapEntry(k, v.toString()));
    }).catchError((_) {});
    rootBundle.loadString('assets/i18n/en_US.json').then((s) {
      final m = Map<String, dynamic>.from(json.decode(s));
      _keys['en_US'] = m.map((k, v) => MapEntry(k, v.toString()));
    }).catchError((_) {});
  }
}
